import createElement from "./createElement";
import patchVnode from "./patchVnode";

function isSameNode (newNode, oldNode) {
    return newNode.sel === oldNode.sel && newNode.data.key === oldNode.data.key
}

export default function updateChildren (newCh, oldCh) {
    let newStartIndex = 0; // 新节点的开始指针
    let newEndIndex = newCh.length - 1; // 新节点的结束指针
    let oldStartIndex = 0; // 旧节点的开始指针
    let oldEndIndex = oldCh.length - 1; // 旧节点的结束指针
    let newStartNode = newCh[newStartIndex]; // 新的开始节点
    let newEndNode = newCh[newEndIndex]; // 新结束节点
    let oldStartNode = oldCh[oldStartIndex]; // 旧的开始节点
    let oldEndNode = oldCh[oldEndIndex]; // 旧的结束节点
    while (newStartIndex <= newEndIndex && oldStartIndex <= oldEndIndex) {
        if (!oldStartNode.elm) {
            ++oldStartIndex;
        } else if (isSameNode(newStartNode, oldStartNode)) {
            // 1. 新的开始节点等于旧的开始节点
            console.log("1. 新的开始节点等于旧的开始节点");
            patchVnode(newStartNode, oldStartNode);
            newEndNode.elm = oldEndNode.elm;
            newStartNode = newCh[++newStartIndex];
            oldStartNode = oldCh[++oldStartIndex];
        } else if (isSameNode(newEndNode, oldEndNode)) {
            // 2. 新的结束节点等于旧的结束节点
            console.log("2. 新的结束节点等于旧的结束节点")
            patchVnode(newEndNode, oldEndNode);
            newEndNode.elm = oldEndNode.elm;
            newEndNode = newCh[--newEndIndex];
            oldEndNode = oldCh[--oldEndIndex];
        } else if (isSameNode(newEndNode, oldStartNode)) {
            // 3. 新的结束节点等于新的开始节点
            console.log("3. 新的结束节点等于新的开始节点");
            patchVnode(newEndNode, oldStartNode);
            newEndNode.elm = oldStartNode.elm;
            // 旧节点的开始节点需要移动到旧节点最后一个节点的后一个
            oldStartNode.elm.parentNode.insertBefore(oldStartNode.elm, oldEndNode.elm.sibling);
            newEndNode = newCh[--newEndIndex];
            oldStartNode = oldCh[++oldStartIndex];
        } else if (isSameNode(newStartNode, oldEndNode)) {
            // 4. 新的开始节点等于旧的结束节点
            console.log("4. 新的开始节点等于旧的结束节点");
            patchVnode(newStartNode, oldEndNode);
            newStartNode.elm = oldEndNode.elm;
            oldEndNode.elm.parentNode.insertBefore(oldEndNode.elm, oldStartNode.elm);
            newStartNode = newCh[++newStartIndex];
            oldEndNode = oldCh[--oldEndIndex];
        } else {
            // 5. 乱序查找对比并且移动节点
            console.log("5. 乱序查找对比并且移动节点");
            const keyMap = {};
            for (let i = 0; i < oldCh.length; i++) {
                const { key } = oldCh[i].data;
                keyMap[key] = oldCh[i];
            }
            debugger;
            // 新开始节点的key值
            const newKey = newStartNode.data.key;
            // 新节点在老节点中的位置
            const oldNode = keyMap[newKey];
            if (oldNode) {
                // 说明新节点在老节点中已经存在
                patchVnode(newStartNode, oldNode);
                // 将老节点移动至开始的位置
                newStartNode.elm = oldNode.elm;
                oldNode.elm.parentNode.insertBefore(oldNode.elm, oldStartNode.elm);
                oldNode.elm = undefined;
            } else {
                // 说明新节点在老节点中不存在
                oldStartNode.elm.parentNode.insertBefore(createElement(newStartNode), newStartNode.elm.nextSlibing);
            }
            newStartNode = newCh[++newStartIndex];
        }
    }
    // 如果老节点中有不用的节点
    if (oldStartIndex >= oldEndIndex) {
        for (let i = oldStartIndex; i <= oldEndIndex; i++) {
            oldStartIndex++;
            if (!oldStartNode) return;
            oldStartNode.elm.parentNode.removeChild(oldStartNode.elm);
        }
    }
    // 如果新节点中有没添加上的节点
    if (newStartIndex > newEndIndex) {
        console.log("如果新节点中有没添加上的节点");
    }

}